package com.mysoft.gfyx.marketdevelop.service.impl;

import com.mysoft.framework.common.exception.BusinessLogicException;
import com.mysoft.framework.service.AppService;
import com.mysoft.gfyx.marketdevelop.controller.dto.GetDetailDataRequestDto;
import com.mysoft.gfyx.marketdevelop.mip.GetDeveloperDataService;
import com.mysoft.gfyx.marketdevelop.mip.dto.*;
import com.mysoft.gfyx.marketdevelop.service.CommonService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperSaleProjectService;
import com.mysoft.gfyx.marketdevelop.service.DeveloperService;
import com.mysoft.gfyx.marketdevelop.service.LoadDataApiCommonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

@Service
public class DeveloperSaleProjectServiceImp extends AppService implements DeveloperSaleProjectService {
    @Autowired
    private DeveloperService developerService;

    @Autowired
    private GetDeveloperDataService getDeveloperDataService;

    @Autowired
    private CommonService commonService;

    public Object getProjectDetail(GetDetailDataRequestDto dto) {
        //项目详情
        GetDeveloperFilterDataRequestDto requestDto = new GetDeveloperFilterDataRequestDto();
        requestDto = developerService.setCommonData(requestDto);
        requestDto.setDetail_table_name(DeveloperService.TABLE_OPENED_TRADING);
        requestDto.setPage(1);
        requestDto.setPage_size(1);
        requestDto.addFilterDtos("id", BaseTjFilterDto.OPERATE_EQUAL, dto.getId(), DeveloperService.TABLE_OPENED_TRADING, BaseTjFilterDto.ASSOCIATE_NONE);
        BaseTjListResponseDto res = getDeveloperDataService.getFilteredDeveloperDetailList(requestDto);
        if (res.getData().getList() == null || res.getData().getList().size() == 0) {
            throw new BusinessLogicException("未找到项目信息");
        }

        Map<String, Object> data = res.getData().getList().get(0).getItem();
        //公司和顶级公司
        if (data.get("organizing_code") != null) {
            Map<String, Object> companyData = developerService.getDeveloperMainData(data.get("organizing_code").toString());
            data.put("company", companyData);

            if (companyData.get("is_top_company_contain_developer") != null && companyData.get("top_organizing_code") != null && companyData.get("is_top_company_contain_developer").equals("是")) {
                Map<String, Object> topCompanyData = developerService.getDeveloperMainData(companyData.get("top_organizing_code").toString());
                data.put("topCompany", topCompanyData);
            }
        }

        if (data.get("new_house_deal_money") == null || data.get("new_house_deal_money").equals("")) {
            data.put("new_house_deal_money", "--");
        } else {
            data.put("new_house_deal_money", data.get("new_house_deal_money") + "万元");
        }

        if (data.get("new_house_deal_num") == null || data.get("new_house_deal_num").equals("")) {
            data.put("new_house_deal_num", "--");
        } else {
            data.put("new_house_deal_num", data.get("new_house_deal_num") + "套");
        }

        if (data.get("new_house_sale_rate") == null || data.get("new_house_sale_rate").equals("")) {
            data.put("new_house_sale_rate", "--");
        } else {
            data.put("new_house_sale_rate", data.get("new_house_sale_rate") + "%");
        }

        //数据状态
        if (data.get("id") != null) {
            data.put("oppty_status", commonService.getOpptyStatus(CommonService.BigDataType.SALE.toString(), data.get("id").toString(), true));
        }
        return data;
    }
}
